Object.defineProperty(exports, "__esModule", {
  value: true
});
var $z1SoundManager = require("SoundManager");
var $z1Tool = require("Tool");
var $z1Pool = require("Pool");
var $z1BattleGround2Ctrl = require("BattleGround2Ctrl");
var $z1EnemySpawner2 = require("EnemySpawner2");
var $z1Num2 = require("Num2");
var cc__decorator = cc._decorator;
var ccp_ccclass = cc__decorator.ccclass;
var ccp_property = cc__decorator.property;
var def_Role2 = function (e) {
  function _ctor() {
    var t = null !== e && e.apply(this, arguments) || this;
    t.body = null;
    t.bulletSPos = null;
    t.bodySpine = null;
    t.bodySDS = [];
    t.collBody = null;
    t.team = 0;
    t.tCount = 0;
    t.v = 50;
    t.par = null;
    t.bulletConfig = {
      rate: 0,
      nums: 0,
      dmg: 0
    };
    t.hp = 100;
    t.state = 0;
    t.hpY = 0;
    t.isDragging = false;
    t.dXYRec = [0, 0];
    t.hpNumberHandle = null;
    return t;
  }
  __extends(_ctor, e);
  _ctor.prototype.init = function (e) {
    undefined === e && (e = {});
    this.team = e.team;
    this.par = e.par;
    if (0 === this.team) {
      this.collBody.enabled = false;
      this.bulletConfig = {
        rate: $z1BattleGround2Ctrl.default.JSONManager.ConfigBattle[6].v[5],
        nums: $z1BattleGround2Ctrl.default.JSONManager.ConfigBattle[6].v[3],
        dmg: $z1BattleGround2Ctrl.default.JSONManager.ConfigBattle[6].v[4]
      };
      this.initDragEvent();
    } else {
      this.bodySpine.skeletonData = this.bodySDS[e.rId];
      this.bodySpine.setAnimation(0, "walk", true);
      this.hp = e.config.hp;
      this.bodySpine.node.setScale(e.config.spineSize);
      this.hpY = e.config.hpy;
      this.v = $z1BattleGround2Ctrl.default.JSONManager.ConfigBattle[6].v[2];
      this.createNewHpNum();
    }
    this.par.refreshAtk();
  };
  _ctor.prototype.initDragEvent = function () {
    var e = this;
    this.body.on(cc.Node.EventType.TOUCH_START, function (t) {
      e.isDragging = true;
      e.dXYRec[0] = t.getLocationX();
      e.dXYRec[1] = t.getLocationY();
    });
    this.body.on(cc.Node.EventType.TOUCH_MOVE, function (t) {
      var o = t.getLocationX();
      var a = t.getLocationY();
      e.node.x += o - e.dXYRec[0];
      if (e.node.x < -300) {
        e.node.x = -300;
      } else {
        e.node.x > 300 && (e.node.x = 300);
      }
      e.dXYRec[0] = o;
      e.dXYRec[1] = a;
    });
    this.body.on(cc.Node.EventType.TOUCH_END, function () {
      e.isDragging = false;
    });
    this.body.on(cc.Node.EventType.TOUCH_CANCEL, function () {
      e.isDragging = false;
    });
  };
  _ctor.prototype.createNewBullet = function (e) {
    if (10 === this.par.state && (this.tCount += e, !(this.tCount < this.bulletConfig.rate))) {
      this.tCount -= this.bulletConfig.rate;
      var t = $z1BattleGround2Ctrl.default.JSONManager.ConfigBattle[6].v[7];
      var o = this.node.x + this.bulletSPos.x - Math.floor(this.bulletConfig.nums / 2) * t;
      var a = this.node.y + this.bulletSPos.y;
      for (var n = 0; n < this.bulletConfig.nums; n++) {
        this.par.createNewBullet(o, a, {
          dmg: this.bulletConfig.dmg
        });
        o += t;
      }
      this.bodySpine.setAnimation(1, "atk", false);
    }
  };
  _ctor.prototype.createNewHpNum = function () {
    this.hpNumberHandle = this.par.createNewHpNum(this);
  };
  _ctor.prototype.moving = function (e) {
    if (10 === this.par.state) {
      this.node.y -= this.v * e;
      if (this.node.y < this.par.endLineY + this.node.height / 2) {
        this.par.beHit(), this.remove();
      }
    }
  };
  _ctor.prototype.beHit = function (e) {
    this.hp -= e.dmg;
    if (this.hp <= 0) {
      this.par.createNewDieEff(this.node);
      this.par.node.getComponent($z1EnemySpawner2.default).updateEnemyHandles(this.node.x);
      this.remove();
      $z1SoundManager.default.playEffect2($z1Tool.default.randomInt(3, 5));
    } else {
      this.hpNumberHandle.getComponent($z1Num2.default).changeHpTo();
    }
  };
  _ctor.prototype.remove = function () {
    this.hpNumberHandle.parent.getComponent($z1Pool.default).destroyPoolItem(this.hpNumberHandle);
    this.node.parent.getComponent($z1Pool.default).destroyPoolItem(this.node);
  };
  _ctor.prototype.start = function () {};
  _ctor.prototype.update = function (e) {
    if (0 === this.team) {
      this.createNewBullet(e);
    } else {
      this.moving(e);
    }
  };
  __decorate([ccp_property(cc.Node)], _ctor.prototype, "body", undefined);
  __decorate([ccp_property(cc.Node)], _ctor.prototype, "bulletSPos", undefined);
  __decorate([ccp_property(sp.Skeleton)], _ctor.prototype, "bodySpine", undefined);
  __decorate([ccp_property(sp.SkeletonData)], _ctor.prototype, "bodySDS", undefined);
  __decorate([ccp_property(cc.BoxCollider)], _ctor.prototype, "collBody", undefined);
  return __decorate([ccp_ccclass], _ctor);
}(cc.Component);
exports.default = def_Role2;